package com.example.socialapp.ui.main.screens

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.LocationOn
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.filled.Toys
import androidx.compose.material.icons.filled.TrendingUp
import androidx.compose.material.icons.outlined.Bookmarks
import androidx.compose.material.icons.outlined.FitnessCenter
import androidx.compose.material.icons.outlined.LiveTv
import androidx.compose.material.icons.outlined.LocalActivity
import androidx.compose.material.icons.outlined.Restaurant
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Divider
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import kotlin.math.absoluteValue

/**
 * 发现Tab界面
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DiscoverScreen() {
    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text("发现") },
                actions = {
                    IconButton(onClick = { /* 搜索 */ }) {
                        Icon(
                            imageVector = Icons.Default.Search,
                            contentDescription = "搜索"
                        )
                    }
                },
                colors = TopAppBarDefaults.topAppBarColors(
                    containerColor = MaterialTheme.colorScheme.primaryContainer,
                    titleContentColor = MaterialTheme.colorScheme.onPrimaryContainer
                )
            )
        }
    ) { innerPadding ->
        LazyColumn(
            modifier = Modifier
                .fillMaxSize()
                .padding(innerPadding),
            contentPadding = PaddingValues(bottom = 16.dp)
        ) {
            // 功能模块
            item {
                FunctionModules()
                Divider(modifier = Modifier.padding(vertical = 8.dp))
            }
            
            // 附近的人
            item {
                SectionTitle(title = "附近的人", icon = Icons.Default.LocationOn)
                NearbyPeopleSection()
                Divider(modifier = Modifier.padding(vertical = 8.dp))
            }
            
            // 热门话题
            item {
                SectionTitle(title = "热门话题", icon = Icons.Default.TrendingUp)
                HotTopicsSection()
                Divider(modifier = Modifier.padding(vertical = 8.dp))
            }
            
            // 推荐活动
            item {
                SectionTitle(title = "推荐活动", icon = Icons.Default.Toys)
                RecommendedActivitiesSection()
            }
        }
    }
}

/**
 * 功能模块区域
 */
@Composable
private fun FunctionModules() {
    Column(modifier = Modifier.padding(vertical = 8.dp)) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 16.dp),
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            FunctionItem(
                icon = Icons.Outlined.LiveTv,
                title = "直播",
                modifier = Modifier.weight(1f)
            )
            FunctionItem(
                icon = Icons.Outlined.Restaurant,
                title = "游戏",
                modifier = Modifier.weight(1f)
            )
            FunctionItem(
                icon = Icons.Outlined.Restaurant,
                title = "美食",
                modifier = Modifier.weight(1f)
            )
            FunctionItem(
                icon = Icons.Outlined.Bookmarks,
                title = "收藏",
                modifier = Modifier.weight(1f)
            )
        }
        
        Spacer(modifier = Modifier.height(16.dp))
        
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 16.dp),
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            FunctionItem(
                icon = Icons.Outlined.LocalActivity,
                title = "活动",
                modifier = Modifier.weight(1f)
            )
            FunctionItem(
                icon = Icons.Outlined.FitnessCenter,
                title = "运动",
                modifier = Modifier.weight(1f)
            )
            // 空白占位
            Spacer(modifier = Modifier.weight(1f))
            Spacer(modifier = Modifier.weight(1f))
        }
    }
}

/**
 * 功能模块项
 */
@Composable
private fun FunctionItem(
    icon: ImageVector,
    title: String,
    modifier: Modifier = Modifier
) {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = modifier.clickable { /* 点击处理 */ }
    ) {
        Box(
            modifier = Modifier
                .size(48.dp)
                .clip(CircleShape)
                .background(MaterialTheme.colorScheme.secondaryContainer),
            contentAlignment = Alignment.Center
        ) {
            Icon(
                imageVector = icon,
                contentDescription = title,
                tint = MaterialTheme.colorScheme.onSecondaryContainer
            )
        }
        
        Spacer(modifier = Modifier.height(4.dp))
        
        Text(
            text = title,
            style = MaterialTheme.typography.bodySmall
        )
    }
}

/**
 * 章节标题
 */
@Composable
private fun SectionTitle(title: String, icon: ImageVector) {
    Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 16.dp, vertical = 8.dp)
    ) {
        Icon(
            imageVector = icon,
            contentDescription = null,
            tint = MaterialTheme.colorScheme.primary,
            modifier = Modifier.size(20.dp)
        )
        
        Spacer(modifier = Modifier.width(8.dp))
        
        Text(
            text = title,
            style = MaterialTheme.typography.titleMedium,
            color = MaterialTheme.colorScheme.onSurface
        )
    }
}

/**
 * 附近的人区域
 */
@Composable
private fun NearbyPeopleSection() {
    LazyRow(
        contentPadding = PaddingValues(horizontal = 16.dp),
        horizontalArrangement = Arrangement.spacedBy(12.dp)
    ) {
        items(nearbyPeople) { person ->
            NearbyPersonItem(person = person)
        }
    }
}

/**
 * 附近的人项
 */
@Composable
private fun NearbyPersonItem(person: DiscoverPerson) {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = Modifier.width(72.dp)
    ) {
        Box(
            modifier = Modifier
                .size(64.dp)
                .clip(CircleShape)
                .background(getColorForPerson(person))
        ) {
            Text(
                text = person.name.first().toString(),
                style = MaterialTheme.typography.titleLarge,
                color = Color.White,
                modifier = Modifier.align(Alignment.Center)
            )
        }
        
        Spacer(modifier = Modifier.height(4.dp))
        
        Text(
            text = person.name,
            style = MaterialTheme.typography.bodySmall,
            maxLines = 1,
            overflow = TextOverflow.Ellipsis
        )
        
        Text(
            text = "${person.distance}米",
            style = MaterialTheme.typography.labelSmall,
            color = MaterialTheme.colorScheme.outline
        )
    }
}

/**
 * 热门话题区域
 */
@Composable
private fun HotTopicsSection() {
    LazyRow(
        contentPadding = PaddingValues(horizontal = 16.dp),
        horizontalArrangement = Arrangement.spacedBy(12.dp)
    ) {
        items(hotTopics) { topic ->
            TopicItem(topic = topic)
        }
    }
}

/**
 * 话题项
 */
@Composable
private fun TopicItem(topic: Topic) {
    Card(
        modifier = Modifier.width(160.dp),
        shape = RoundedCornerShape(8.dp),
        colors = CardDefaults.cardColors(
            containerColor = MaterialTheme.colorScheme.secondaryContainer
        )
    ) {
        Column(
            modifier = Modifier.padding(12.dp)
        ) {
            Text(
                text = "#${topic.title}",
                style = MaterialTheme.typography.titleSmall,
                fontWeight = FontWeight.Bold,
                maxLines = 2,
                overflow = TextOverflow.Ellipsis
            )
            
            Spacer(modifier = Modifier.height(4.dp))
            
            Text(
                text = "${topic.discussions}人正在讨论",
                style = MaterialTheme.typography.bodySmall,
                color = MaterialTheme.colorScheme.outline
            )
        }
    }
}

/**
 * 推荐活动区域
 */
@Composable
private fun RecommendedActivitiesSection() {
    Column(
        modifier = Modifier.padding(horizontal = 16.dp)
    ) {
        activities.forEach { activity ->
            ActivityItem(activity = activity)
            if (activity != activities.last()) {
                Divider(modifier = Modifier.padding(vertical = 8.dp))
            }
        }
    }
}

/**
 * 活动项
 */
@Composable
private fun ActivityItem(activity: Activity) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clickable { /* 查看活动详情 */ }
            .padding(vertical = 8.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        // 活动封面
        Box(
            modifier = Modifier
                .width(120.dp)
                .aspectRatio(16f / 9f)
                .clip(RoundedCornerShape(8.dp))
                .background(getColorForActivity(activity))
        ) {
            Text(
                text = activity.title.first().toString(),
                style = MaterialTheme.typography.headlineMedium,
                color = Color.White,
                modifier = Modifier.align(Alignment.Center)
            )
        }
        
        Spacer(modifier = Modifier.width(12.dp))
        
        // 活动信息
        Column(modifier = Modifier.weight(1f)) {
            Text(
                text = activity.title,
                style = MaterialTheme.typography.titleMedium,
                maxLines = 1,
                overflow = TextOverflow.Ellipsis
            )
            
            Spacer(modifier = Modifier.height(4.dp))
            
            Text(
                text = activity.time,
                style = MaterialTheme.typography.bodySmall,
                color = MaterialTheme.colorScheme.outline
            )
            
            Spacer(modifier = Modifier.height(4.dp))
            
            Text(
                text = "${activity.participants}人参加",
                style = MaterialTheme.typography.bodySmall,
                color = MaterialTheme.colorScheme.primary
            )
        }
    }
}

/**
 * 根据人名生成颜色
 */
private fun getColorForPerson(person: DiscoverPerson): Color {
    val colors = listOf(
        Color(0xFF6200EA), // 深紫色
        Color(0xFF2962FF), // 深蓝色
        Color(0xFF00BFA5), // 青色
        Color(0xFFFFAB00), // 琥珀色
        Color(0xFFD50000)  // 红色
    )
    
    return colors[person.name.hashCode().mod(colors.size).absoluteValue]
}

/**
 * 根据活动名称生成颜色
 */
private fun getColorForActivity(activity: Activity): Color {
    val colors = listOf(
        Color(0xFF6200EA), // 深紫色
        Color(0xFF2962FF), // 深蓝色
        Color(0xFF00BFA5), // 青色
        Color(0xFFFFAB00), // 琥珀色
        Color(0xFFD50000)  // 红色
    )
    
    return colors[activity.title.hashCode().mod(colors.size).absoluteValue]
}

/**
 * 发现页面人物数据类
 */
data class DiscoverPerson(
    val name: String,
    val distance: Int // 距离（米）
)

/**
 * 话题数据类
 */
data class Topic(
    val title: String,
    val discussions: Int // 讨论人数
)

/**
 * 活动数据类
 */
data class Activity(
    val title: String,
    val time: String,
    val participants: Int // 参与人数
)

/**
 * 示例附近的人数据
 */
private val nearbyPeople = listOf(
    DiscoverPerson("张三", 50),
    DiscoverPerson("李四", 120),
    DiscoverPerson("王五", 200),
    DiscoverPerson("赵六", 350),
    DiscoverPerson("钱七", 500),
    DiscoverPerson("孙八", 750)
)

/**
 * 示例热门话题数据
 */
private val hotTopics = listOf(
    Topic("2024年春节晒美食", 2458),
    Topic("户外徒步攻略", 1256),
    Topic("最近爱看的电影", 3721),
    Topic("年终总结分享", 986),
    Topic("学习计划打卡", 1542)
)

/**
 * 示例活动数据
 */
private val activities = listOf(
    Activity("城市定向越野挑战赛", "本周六 09:00-17:00", 128),
    Activity("摄影爱好者线下交流会", "下周日 14:00-16:30", 56),
    Activity("编程技术分享沙龙", "2024-02-20 19:00-21:00", 74)
) 